﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Red.Controls.Converters
{
    /// <summary>
    ///    An enum used to set a modifier for updating a numeric value
    /// </summary>
    public enum ToolTipSection
    {
        Header,
        Description,
        Link
    }

    public class ToolTipTextConverter : IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            if ( value is string )
                return SectionText( (string)value, (ToolTipSection)parameter );
            else
                return value;
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return value;
        }

        public static string SectionText( string value, ToolTipSection section )
        {
            int index = 0;

            switch ( section )
            {
                case ToolTipSection.Header:
                    index = 0;
                    break;
                case ToolTipSection.Description:
                    index = 1;
                    break;
                case ToolTipSection.Link:
                    index = 2;
                    break;
            }

            string [] toolTipParts = value.Split('|');

            if ( toolTipParts.Length == 0 )
                return value;
            else if ( toolTipParts.Length == 1 )
                return section == ToolTipSection.Description ? value : string.Empty; // default to Description for plain text without sections
            if ( index < toolTipParts.Length )
                return toolTipParts[ index ];
            else
                return string.Empty;
        }
    }
}
